<?php
namespace App\Service\ApiXcx;

use Illuminate\Support\Facades\DB;

class PointsGoodsService extends BaseService
{
    //商品

    /**
     * 分类列表
     * @return array
     */
    public function getCateList()
    {
        $db = DB::table($this->dbPointsGcate)
            ->where('del', 0)
            ->where('pid', 0);
        $total = $db->count();
        $res = $db->skip(0)->take(4)->get();
        if (!count($res)) {
            return array('code' => 0, 'msg' => '没有数据', 'data' => [], 'total' => 0);
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $imglink = $v['img'] ? env('DOMAIN') . '/' . $v['img'] : '';
            $arys[] = [
                'id' => $v['id'],
                'img' => $imglink,
                'title' => $v['title'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    /**
     * 商品列表
     * @param array $pages【start，limit】
     * @param int $cateid 分类ID
     * @param array $pas【is_sale，is_price，is_new】
     * @param int $uid
     * @return array
     */
    public function getGoodsList($pages, $cateid, $keyword, $pas = [], $uid = 0)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbPointsGoods)->where('del', 0);
        if ($cateid) {
            $db = $db->where('cate_id', $cateid);
        }
        if ($keyword) {
            $db = $db->where('title', 'like', '%'.$keyword.'%');
        }
        if (isset($pas['is_new']) && $pas['is_new']) {
            $db = $db->where('isnew', 'like',1);
        }
        $total = $db->count();
        $db = $db->skip($start)->take($pages['limit']);
        if (isset($pas['is_sale']) && $pas['is_sale']) {
            $isSale = $pas['is_sale']==1 ? 'desc' : 'asc';
            $db = $db->orderBy('sale', $isSale);
        }
        if (isset($pas['is_price']) && $pas['is_price']) {
            $isPrice = $pas['is_price']==1 ? 'desc' : 'asc';
            $db = $db->orderBy('price', $isPrice);
        }
        $res = $db->get();
        if (!count($res)) {
            return array('code' => 0, 'msg' => '没有数据', 'data' => [], 'total' => $total);
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //分类
            $cateOne = (array) DB::table($this->dbPointsGcate)->where('id', $v['id'])->first();
            //价格
            $skuid = 0; $price = 0; $price_line = 0;
            $skuOne = (array) DB::table($this->dbPointsGsku)
                ->where('gid', $v['id'])
                //->orderBy('price', 'asc')
                ->first();
            if (!empty($skuOne)) {
                $skuid = $skuOne['id'];
                $price = $skuOne['price'];
                $price_line = $skuOne['price_line']/100;
            }
            $arys[] = [
                'id' => $v['id'],
                'gno' => $v['gno'],
                'title' => $v['title'],
                'cate_id' => $v['cate_id'],
                'cateName' => $cateOne ? $cateOne['title'] : '',
                'img' => env('DOMAIN'). '/' . $v['bigimg'],
                'sku_id' => $skuid,
                'stock' => $skuOne['stock'],
                'price' => $price,
                'price_line' => $price_line,
                'sale' => $v['sale'],
                'isnew' => $v['isnew'],
                'part' => 'chengfen',
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    //商品选项
    public function getOption()
    {
        $arys = ['delivery' => [], 'shop' => []];
        //配送方式
        $deliverys = $this->orderDeliverys;
        foreach ($deliverys as $k => $v) {
            $arys['delivery'][] = [
                'id' => $k,
                'title' => $v,
            ];
        }
        //门店
        $shopOnes = DB::table($this->dbShop)->where('del', 0)->get();
        if (count($shopOnes)) {
            foreach ($shopOnes as $v) {
                $v = (array) $v;
                $arys['shop'][] = [
                    'id' => $v['id'],
                    'title' => $v['shop_name'] . '-' . $v['address'],
                ];
            }
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    //商品详情
    public function getGoodsOne($gid)
    {
        $arys['gid'] = $gid;
        $arys['gprice'] = 0;
        $arys['gprice_line'] = 0;
        $arys['gsale'] = 0;
        //商品详情
        $gOne = (array) DB::table($this->dbPointsGoods)->where('id', $gid)->first();
        if (!empty($gOne)) {
            $arys['gname'] = $gOne['title'];
            $arys['gprice'] = $gOne['price'];
            $arys['gsale'] = $gOne['sale'];
            $arys['gdetail'] = $gOne['detail'] ? htmlspecialchars_decode($gOne['detail']) : '';
        }
        //商品图片
        $gImgs = DB::table($this->dbPointsGimg)->where('del', 0)->where('gid', $gid)->get();
        if (count($gImgs)) {
            foreach ($gImgs as $v) {
                $v = (array) $v;
//                $arys['gimgs'][] = [
//                    'img' => env('DOMAIN') . '/' . $v['link'],
//                ];
                $arys['gimgs'][] = env('DOMAIN') . '/' . $v['link'];
            }
        }
        //SKU
        $skuOnes = DB::table($this->dbPointsGsku)->where('del', 0)->where('gid', $gid)->get();
        if (count($skuOnes)) {
            $gprice = 0; $gprice_line = 0;
            foreach ($skuOnes as $v) {
                $v = (array) $v;
                $arys['skuList'][] = [
                    'sku_id' => $v['id'],
                    'sku_name' => $v['title'],
                    'price' => $v['price'],
                    'price_line' => $v['price_line']/100,
                ];
                if (!$gprice && $v['price']) {
                    $gprice = $v['price'];
                    $gprice_line = $v['price_line']/100;
                } else if ($gprice && $gprice > $v['price']) {
                    $gprice = $v['price'];
                    $gprice_line = $v['price_line']/100;
                }
            }
            $arys['gprice'] = $gprice;
            $arys['gprice_line'] = $gprice_line;
            if ((isset($gOne['price']) && !$gOne['price']) && $arys['gprice']) {
                DB::table($this->dbPointsGoods)->where('id', $gid)->update(['price'=>$arys['gprice']]);
            }
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * SKU列表
     * @param array $pages【start，limit】
     * @param int $gid
     * @return array
     */
    public function getSkuList($pages, $gid)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbPointsGsku)->where('del', 0)->where('gid', $gid);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return array('code' => 0, 'msg' => '没有数据', 'data' => []);
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'sku_id' => $v['id'],
                'title' => $v['title'],
                'price' => $v['price'],
                'price_line' => $v['price_line']/100,
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }
}